package com.chara.rabbitmq_demo.com.chara.rabbitmq.utils;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @Classname ConnectionUtil
 * @Description 连接工具类
 * @Date 2025/10/8 21:13
 * @Author Chara
 */
public class ConnectionUtil {
    public static final String HOST_ADDRESS = "127.0.0.1";
    public static Connection getConnection() throws Exception {
        // 定义连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();

        // 设置服务地址
        connectionFactory.setHost(HOST_ADDRESS);

        // 设置端口
        connectionFactory.setPort(5672);

        // 设置虚拟主机、账户、密码
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

        // 通过工厂获取连接
        Connection connection = connectionFactory.newConnection();

        return connection;
    }

    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        System.out.println(connection);
        connection.close();
    }

}
